<script setup lang="ts">
import { useLoadMore } from 'vue-request'
import { fetchInviteListAPI } from '@/apis/fetchInviteList'
import { useUserInfoStore } from '@/store/userInfo'

const userInfoStore = useUserInfoStore()

// 分页列表数据
const { dataList, loading, loadingMore, noMore, loadMore } = useLoadMore<LoadMoreData<InviteRecord>>(
  fetchData,
  { isNoMore: d => d?.list.length === d?.total },
)

// “加载更多”的状态
const loadMoreStatus = computed(() => {
  if (loadingMore.value)
    return 'loading'
  else if (noMore.value)
    return 'nomore'
  else
    return 'loadmore'
})

onReachBottom(() => loadMore())

/**
 * @description 分页服务函数
 */
async function fetchData(d?: LoadMoreData<InviteRecord>): Promise<LoadMoreData<InviteRecord>> {
  // 当前页数
  const _page = d?.page ? d.page + 1 : 1

  const res = await fetchInviteListAPI(
    { page: _page.toString(), limit: '10' },
    userInfoStore.userInfo!.token!,
  )

  return {
    list: res.data?.data.data || [],
    page: _page,
    total: res.data?.data.total || 9999,
  }
}
</script>

<template>
  <Spacer height="45" />
  <view v-if="dataList.length > 0">
    <InviteItem
      v-for="item in dataList"
      :key="item.id"
      :consumed="item.is_free"
      :invited-user="item.nickname"
      :invite-time="item.create_time"
      :arrival-time="item.free_time"
    />
    <!-- 加载更多 -->
    <uv-load-more v-if="dataList.length > 3" :status="loadMoreStatus" @loadmore="loadMore" />
    <Spacer height="240" />
  </view>
  <PageDefault v-else>
    暂无邀请记录
  </PageDefault>
  <uv-loading-page :loading="loading && !loadingMore" loading-text="加载中..." font-size="28rpx" loading-mode="spinner" />
</template>

<route lang="yaml">
name: 'inviteRecord'
style:
  navigationBarTitleText: '邀请记录'
</route>
